#!/usr/local/bin/perl

# leak < <lsof_leak_debug_output>

while (<>) {
    if (!/^MEM[afr]/) { next; }
    chop;
    if (/^MEMa ([^ ]*) (.*)/) {
	$new = $1; $loc = $2;
	if (defined($mem{$new})) {
	    print "ERROR: $new already allocated;\n";
	    print "  OLD: $mem{$new}\n";
	    print "  NEW: $_\n";
	} else {$mem{$new} = $loc; }
	next;
    } elsif (/^MEMf ([^ ]*) (.*)/) {
	$old = $1; $loc = $2;
	if (!defined($mem{$old})) {
	    print "ERROR: $old not allocated: $_\n";
	} else { undef($mem{$old}); }
	next;
    } elsif (/^MEMr ([^ ]*) ([^ ]*) (.*)/) {
	$old = $1; $new = $2; $loc = $3;
	if (!defined($mem{$old})) {
	    print "ERROR: $old not allocated: $_\n";
	} else { undef($mem{$old}); }
	if (defined($mem{$new})) {
	    print "ERROR: $new already allocated;\n";
	    print "  OLD: $mem{$new}\n";
	    print "  NEW: $_\n";
	} else {$mem{$new} = $loc; }
	next;
    } else { print "ERROR: MEM what? $_\n"; }
}
foreach $addr (sort keys(%mem)) {
    if (defined($mem{$addr})) {
	if (($mem{$addr} =~ /^rnch.c/)) { next; }
	if (($mem{$addr} =~ /^print.c:14[18]/)) { next; }
	if (($mem{$addr} =~ /^print.c:323/)) { next; }
	if (($mem{$addr} =~ /^print.c:1135/)) { next; }
	if (($mem{$addr} =~ /^print.c:207/)) { next; }
	if (($mem{$addr} =~ /^print.c:219/)) { next; }
	if (($mem{$addr} =~ /^print.c:335/)) { next; }
	if (($mem{$addr} =~ /^print.c:261/)) { next; }
	if (($mem{$addr} =~ /^print.c:1076/)) { next; }
	if (($mem{$addr} =~ /^proc.c:191/)) { next; }
	if (($mem{$addr} =~ /^main.c:581/)) { next; }
	if (($mem{$addr} =~ /^rvfs.c:68/)) { next; }
	print "IN USE $addr: $mem{$addr}\n";
    }
}
